// pages/detail/dragon.js
import authorize from '../../utils/authorize.js';
import Router from '../../utils/router';
import util from '../../utils/util.js';
import goods from '../../services/goods.js';
import Record from '../../utils/page-record.js';
import formid from '../../services/formid.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    segsid: '', //接龙id
    store_id: '', //店铺id;
    datas: {},
    s: '',
    h: '',
    m: '', //时分秒
    page: 1, //页码
    offset: 20, //页量
    orderlist: [],
    count: '',
    step: '', //传给进度的字段
    checks: '',
    priceall: [], //进度
    phone: '', //区分手机
    quane: '',
    // is_allow_conch: '',
    conch_num: '',
    special: '',
    share_url: '', //分享出去的图片
    end: false, //时间到了 结束
    no: false, //抢光了
    isShowTips: false, //是否在自己店铺提示
    goods_total: '', //当前接龙的人数
    share: '', //显示分享
    list:'1',
    fshow:'',//朋友圈开关
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _this = this;
    this.setData({
      store_id: options.store_id,
      segsid: options.segsid,
      orderlist: []
    })
    wx.getSystemInfo({
      success: function(res) {
        console.log(res.model)
        if (res.model.indexOf('iPhone X') != -1) {
          _this.setData({
            phone: 1
          })
        }
      },
    })
    this.createHtml();
    this.createOrder();
    //在非自己店铺浏览时的提示
    let user_infoStoreId = wx.getStorageSync('user_info').store_id;
    if (_this.data.store_id != user_infoStoreId && user_infoStoreId != null) {
      _this.setData({
        isShowTips: true
      })
    }
    //朋友圈开关
    var list = wx.getStorageSync('activity').share_activity;
    if (list == 1) {
      _this.setData({
        fshow: 1
      })
    } else {
      _this.setData({
        fshow: 0
      })
    }
    this.creatLunbo();
  },
  //获取轮播
  creatLunbo() {
    let params = {};
    let _this = this;
    formid.shufflingorder(params, res => {
      if (res.code == 1) {
        let beging = res.data;
        if (beging.length > 30) {
          beging = beging.slice(0, 30)
        }
        _this.setData({
          beging: beging
        })
      }
    })
  },
  //点击返回自己店铺时方法
  goStore() {
    let user_info = wx.getStorageSync('user_info');
    let store_id = user_info.store_id;
    Router.reLaunch('store', `store_id=${store_id}`, 'reLaunch');
  },
  createHtml() {
    let _this = this;
    let params = {
      store_id: _this.data.store_id,
      segsid: _this.data.segsid
    }
    goods.solitaireGoods(params, res => {
      if (res.code == 1) {
        console.log(res.data.solitaire_goods.share_url)
        _this.setData({
          datas: res.data,
          step: res.data.grad.length,
          priceall: res.data.grad,
          conch_num: res.data.goodsSku[0].conch_num,
          special: res.data.goodsSku[0].special_price,
          quane: res.data.goodsSku[0].sale_price,
          share_url: 'https:' + res.data.solitaire_goods.share_url
        })
        let list = res.data.grad;
        for (var i = 0; i < list.length; i++) {
          if (list[i].goods_grads_price == res.data.grad_current_price) {
            _this.setData({
              checks: i + 1
            })
          }
        }

        //库存为0 时倒计时显示为00:00:00
        if (res.data.solitaire_goods.validity_diff_time > 0) {
          if (res.data.goods.all_num == 0 || res.data.goods.status_id == 7 || res.data.goods.check_status_id == 3 || res.data.goods.check_status_id == 2 || res.data.goods.admin_status_id == 7 || res.data.solitaire_goods.active_status == 2) {
            _this.setData({
              no: true,
              s: '00',
              h: '00',
              m: '00'
            })
          } else {
            _this.downTime(res.data.solitaire_goods.validity_diff_time);
          }
        } else if (res.data.solitaire_goods.validity_diff_time <= 0) {
          _this.setData({
            end: true,
            s: '00',
            h: '00',
            m: '00'
          })
        }

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
      console.log(res)
    })
  },
  //订单详情列表
  createOrder() {
    let _this = this;
    let params = {
      page: _this.data.page,
      offset: _this.data.offset,
      store_id: _this.data.store_id,
      segsid: _this.data.segsid
    }
    goods.getOrderList(params, res => {
      if (res.code == 1) {
        if (res.data.list.length) {
          _this.setData({
            orderlist: _this.data.orderlist.concat(res.data.list),
            count: res.data.count,
            goods_total: res.data.goods_total
          })
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  //点击订单查看更多
  seeMore() {
    // let _this = this;
    // if (this.data.page == 4) {
    //   return false
    // };
    // this.setData({
    //   page: Number(_this.data.page) + 1,
    // })
    // this.createOrder();
  },
  //查看大图
  bigImage(e) {
    var src = e.currentTarget.dataset.src;
    var url = e.currentTarget.dataset.img;
    var fang = [];
    for (var i = 0; i < url.length; i++) {
      fang.push('https:' + url[i].url)
    }
    wx.previewImage({
      current: src,
      urls: fang
    })
  },
  //调取定时器
  downTime(time) {
    let _this = this;
    var thistime = setTimeout(function() {
      time--;
      if (time <= -1) {
        clearTimeout(thistime)
        _this.setData({
          s: '00',
          h: '00',
          m: '00',
        })
        _this.createHtml();
      } else {
        _this.downTime(time)
        _this.setData({
          s: _this.formatSeconds(time).s,
          h: _this.formatSeconds(time).h,
          m: _this.formatSeconds(time).m,
        })
      }
    }, 1000)
  },
  formatSeconds(value) {
    var _this = this;
    var secondTime = parseInt(value); // 秒
    var minuteTime = 0; // 分
    var hourTime = 0; // 小时
    if (secondTime > 60) { //如果秒数大于60，将秒数转换成整数
      //获取分钟，除以60取整数，得到整数分钟
      minuteTime = parseInt(secondTime / 60);
      //获取秒数，秒数取佘，得到整数秒数
      secondTime = parseInt(secondTime % 60);
      //如果分钟大于60，将分钟转换成小时
      if (minuteTime > 60) {
        //获取小时，获取分钟除以60，得到整数小时
        hourTime = parseInt(minuteTime / 60);
        //获取小时后取佘的分，获取分钟除以60取佘的分
        minuteTime = parseInt(minuteTime % 60);
      }
    }
    var time = {
      s: _this.getZero(secondTime),
      m: _this.getZero(minuteTime),
      h: _this.getZero(hourTime)
    }
    return time;
  },
  getZero(num) { //添加0
    var date = '';
    if (num < 10) {
      date = '0' + num
    } else {
      date = num
    }
    return date
  },
  //得到是否点击了接龙
  getshare(e) {
    if (e.detail.share){
      this.setData({
        share: true
      })
    }

  },
  //跳转到活动规则
  routerGuize(){
    let url = encodeURIComponent('https://kpxd.zhengdianhaohuo.com/h5/#/active/win3_825')
    Router.go('web', `url=${url}`)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    let _this = this;
    let datas = _this.data.datas;
    let user_id = wx.getStorageSync('user_info').id

    //抢光（售罄，下线）已过活动时间 都不可进行分享
    if (datas.goods.all_num == 0 || datas.solitaire_goods.validity_diff_time <= 0 || datas.goods.all_num == 0 || datas.goods.status_id == 7 || datas.goods.check_status_id == 3 || datas.goods.check_status_id == 2 || datas.goods.admin_status_id == 7 || datas.solitaire_goods.active_status == 2) {
      return false;
    }
    if (res.from === 'button') {
      console.log(res.target)
    }
    let paths = 'pages/store/index?store_id=' + _this.data.store_id + '&to=' + encodeURIComponent('/pages/detail/dragon?segsid=' + _this.data.segsid + '&store_id=' + _this.data.store_id + '&share_id=' + _this.data.store_id + '&sharerecord=detailcard' + '&user_id=' + user_id);
    return {
      title: '力度太大，我好害怕！多人购买，居然还会退差价！',
      path: paths,
      imageUrl: _this.data.share_url
    }
  }
})